/**********************************************************************************************
This code creates Figure 1.
**********************************************************************************************/

set scheme s1mono

** Bring in the data
use "$DATA_OUT/NMDB_ASMB_respondents", clear
keep if in_default_sample==1
svyset [pweight = analysis_weight]



/**********************************************************************************************
Step 1: Create one dataset for use with several bar charts for adverse shocks
* NOTE!  Retirement and payments on other mortgages were not asked in 2016
**********************************************************************************************/

gen concerns_or_difficulties = 100*(x27==1)


gen cat0         = "All"
local label0     = "All"
local hbar_vars0 "D_All"

gen     cat1 = "EAW" if (EUW_LTV_FHFA_tract==0) //effectively abovewater
replace cat1 = "EUW" if(EUW_LTV_FHFA_tract==1) //effectively underwater
local label1 = "EffectiveEquity"
local hbar_vars1 "D_EffectiveEquityEAW D_EffectiveEquityEUW"


gen cat2     = "Foreclosure" if foreclosure==1
replace cat2 = "Current" if current_by_survey==1 & foreclosure==0
replace cat2 = "Other" if mi(cat2)
local label2 = "PaymentStatus"
local hbar_vars2 "D_PaymentStatusCurrent D_PaymentStatusForeclosure" 

gen cat3     = "FTB" if first_time==1
replace cat3 = "NFTB" if first_time!=1
local label3 = "FirstTimeBuyer"
local hbar_vars3 "D_FirstTimeBuyerFTB D_FirstTimeBuyerNFTB"
local legend3 label(1 "First-Time") label(2 "Not First-Time") position(6)

gen cat4     = "Young" if age<40
replace cat4 = "Medium" if age>=40 & age<60
replace cat4 = "Old" if age>=60
local label4 = "Age"
local hbar_vars4 = "D_AgeYoung D_AgeMedium D_AgeOld"

gen cat5     = "Subprime" if score_orig<620
replace cat5 = "Nonprime" if score_orig>=620 & score_orig<680
replace cat5 = "Prime" if score_orig>=680 & !mi(score_orig)
local label5 = "CreditScore"
local hbar_vars5 "D_CreditScoreSubprime D_CreditScoreNonprime D_CreditScorePrime"


gen cat6 = "SingleBorrower" if num_borrowers==1
replace cat6 = "MultipleBorrowers" if num_borrowers!=1
local label6 = "NumBorrowers"
local hbar_vars6 "D_NumBorrowersSingleBorrower D_NumBorrowersMultipleBorrowers"
local legend6 label(1 "One Borrower") label(2 "Multiple Borrowers") position(6)

gen cat7     = race_ethnic
local label7 = "Race"
local hbar_vars7 "D_RaceWhite D_RaceBlack D_RaceHispanic" 


gen cat8     = "BelowMedianInc" if (income < med_income_hmda)
replace cat8 = "AboveMedianInc" if (income >= med_income_hmda) & !mi(income)
local label8 = "Income"
local hbar_vars8 "D_IncomeBelowMedianInc D_IncomeAboveMedianInc"




foreach cc in 0 1 2 3 4 5 6 7 8 {

	preserve
	* Order the responses in their order of reported frequency
	* NOTE: Responses for NCD defaulters will be missing, and we do *not*
	* want to count them.  Also, for retirement/payments on other mortgages,
	* CD defaulters will have missing responses in 2016, because the 2016
	* survey did not ask those questions.
	gen D_1 = 100 * (x31h==1) if !mi(x31h) // unexpected expenses
	gen D_2 = 100 * (x31a==1) if !mi(x31a) //job loss
	gen D_3 = 100 * (x31j==1) if !mi(x31j) // other large debts
	gen D_4 = 100 * (x31e == 1) if !mi(x31e) // illness, disability, or death
	gen D_5 = 100 * (x31g == 1) if !mi(x31g) // change in mortgage payments
	gen D_6 = 100 * (x31d == 1) if !mi(x31d) // separation or divorce 
	gen D_7 = 100 * (x31c == 1) if !mi(x31c) // business failure
	gen D_8 = 100 * (x31b == 1) if !mi(x31b) // retirement
	gen D_9 = 100 * (x31f == 1) if !mi(x31f) // disasters
	gen D_10 = 100 * (x31i == 1) if !mi(x31i) // payments on other mortgages
	
	
	collapse (mean) D_1 D_2 D_3 D_4 D_5 D_6 D_7 D_8 D_9 D_10 CD_ = concerns_or_difficulties [pweight = analysis_weight], by(cat`cc')
	
	reshape long D_, i(cat`cc') j(reason)
	drop if mi(cat`cc')
	rename D_ D_`label`cc''
	save "$DATA_OUT/processing/shocks_`label`cc''", replace
	restore
	
}





/**********************************************************************************************
Step 2: Use the datasets we created to create a version of Figure 1, by group
**********************************************************************************************/


foreach cc in 0 1 2 3 4 5 6 7 8 {
use "$DATA_OUT/processing/shocks_`label`cc''", clear

	cap drop if cat7=="other"
	cap drop if cat2=="Other"
	
reshape wide D_`label`cc'' CD_, i(reason) j(cat`cc') string


***** For use in the legend, calculate the % of defaulters in each group that are CD
	if `cc' == 0 {
		
		summ CD_
		local CD_: display %2.0f r(mean)

	
		local legend0 label(1 "All Borrowers (`CD_'% CD)") position(6)

		
	}


	if `cc' == 1 {
		
		summ CD_EAW
		local CD_EAW: display %2.0f r(mean)
		summ CD_EUW
		local CD_EUW: display %2.0f r(mean)
	
		local legend1 label(1 "Abovewater (`CD_EAW'% CD)") label(2 "Underwater (`CD_EUW'% CD)") position(6)

		
	}


	if `cc' == 2 {
		
		summ CD_Current
		local CD_Current: display %2.0f r(mean)
		summ CD_Foreclosure
		local CD_Foreclosure: display %2.0f r(mean)
		local legend2 label(1 "Current (`CD_Current'% CD)") label(2 "Foreclosure (`CD_Foreclosure'% CD)") position(6) //label(2 "Other") 
	}


	if `cc' == 4 {
		
		summ CD_Young
		local CD_Young: display %2.0f r(mean)
		summ CD_Medium
		local CD_Medium: display %2.0f r(mean)
		summ CD_Old
		local CD_Old: display %2.0f r(mean)
		local legend4 label(1 "Age < 40 (`CD_Young'% CD)") label(2 "40 <= Age < 60 (`CD_Medium'% CD)") label(3 "Age >= 60 (`CD_Old'% CD)") position(6)

		
	}
	
	
	if `cc' == 5 {

		summ CD_Subprime
		local CD_Subprime: display %2.0f r(mean)
		summ CD_Nonprime
		local CD_Nonprime: display %2.0f r(mean)
		summ CD_Prime
		local CD_Prime: display %2.0f r(mean)
		
		local legend5 label(1 "Score < 620 (`CD_Subprime'% CD)") label(2 "620 <= Score < 680 (`CD_Nonprime'% CD)") label(3 "Score >= 680 (`CD_Prime'% CD)") position(6)

	}
	
	
	if `cc' == 7 {
		
		summ CD_White
		local CD_White: display %2.0f r(mean)
		summ CD_Black
		local CD_Black: display %2.0f r(mean)
		summ CD_Hispanic
		local CD_Hispanic: display %2.0f r(mean)
		
		local legend7 label(1 "White (`CD_White'% CD)") label(2 "Black (`CD_Black'% CD)") label(3 "Hispanic (`CD_Hispanic'% CD)") position(6)
	}
	
	
	if `cc' == 8 {

		summ CD_BelowMedianInc
		local CD_BelowMedianInc: display %2.0f r(mean)
		summ CD_AboveMedianInc
		local CD_AboveMedianInc: display %2.0f r(mean)
	
		local legend8 label(1 "< Tract Median (`CD_BelowMedianInc'% CD)") label(2 ">= Tract Median (`CD_AboveMedianInc'% CD)") position(6)
	

	
	}


******* Now create and export the actual graphs
local reason_labels_ 1 "Unexpected expenses" 2 "Job loss" 3 "Payments on other large debts" 
local reason_labels_ `reason_labels_' 4 "Illness, disability, or death" 5 "Change in mortgage payments" 6  "Separation or divorce" 
local reason_labels_ `reason_labels_' 7 "Business failure" 8  "Retirement" 9 "Disaster affecting the property" 10 "Payments on other mortgages" 


label define reason_labels `reason_labels_'

label values reason reason_labels 


graph hbar `hbar_vars`cc'', over(reason, sort(rank)) ytitle("%") yscale(range(0 80)) ylabel(0(10)80) blabel(bar, format(%9.1f) size(tiny)) legend(`legend`cc''  cols(1) on) graphregion(color(white)) bgcolor(white)  /*title("Shocks")*/

graph export "$OUTPUT/adverse_shocks_`label`cc''.pdf", replace

}






